package jp.co.cos.ittrick.channel.controller.admin;

import java.util.Date;

import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.service.ChapterService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class ChapterPublicController extends Controller {

    private ChapterService chapterService;

    public ChapterPublicController() {
        chapterService = new ChapterService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String channel = asString("channel");
        String chapter = asString("chapter");

        ChapterModel chapterModel = chapterService.getChapterByKey(Integer.valueOf(chapter));
        if(chapterModel.getPublicFlg() != null && chapterModel.getPublicFlg()) {
            chapterModel.setPublicFlg(false);
        }else {
            chapterModel.setPublicFlg(true);
            chapterModel.setPublicDate(new Date());
        }

        chapterService.update(chapterModel);

        return redirect("/admin/channel?channel=" + channel);
    }
}
